package com.jiaquyun.jcyx.jetpack.api

import com.jiaquyun.jcyx.app.JCApplication
import com.jiaquyun.jcyx.base.BaseManager
import com.jiaquyun.jcyx.entity.*
import com.module.lemlin.http.HttpResponseData
import rxhttp.RxHttp
import rxhttp.await


class CartRepository : BaseManager() {

    /**
     * 加入购物车
     * */
    suspend fun cartAdd1(
        goodsId: String, goodsNum: String
    ): CartActionResponse = RxHttp.postForm("/api/cart/add")
        .add("goods_id", goodsId)
        .add("goods_num", goodsNum)
        .add("shop_id", "${JCApplication.userShops?.id?:"55"}")
        .await()

    /**
     * 购物车数量加减
     * */
    suspend fun cartChangeNum1(
        goodsNum: String, cartId: String
    ): CartActionResponse = RxHttp.postForm("/api/cart/changeNum")
        .add("goods_num", goodsNum)
        .add("cart_id", cartId)
        .await()

    /**
     * 购物车列表
     * */
    suspend fun cartIndex(): CartResponse =
        RxHttp.postForm("/api/cart/index")
            .add("shop_id", "${JCApplication.userShops?.id?:"55"}")
            .await()

    /**
     * 加入购物/购物车列表
     *
     * @return 返回购物车id
     * */
    suspend fun cartAdd2(
         goodsId: String, goodsNum: String
    ): CartAddResponse {
        val cartAdd = RxHttp.postForm("/api/cart/add")
            .add("goods_id", goodsId)
            .add("goods_num", goodsNum)
            .add("shop_id", "${JCApplication.userShops?.id?:"55"}")
            .await<CartAddResponse>()
        val goodsInfo = RxHttp.get("/api/goods/goodsInfo")
            .add("id", goodsId)
            .add("shop_id", "${JCApplication.userShops?.id?:"55"}")
            .await<GoodsDetailsResponse>()
            .data
        val test = cartAdd.apply {
            cart_id = goodsInfo.cart_id
            goods_num = goodsNum.toInt()
        }
        return test
    }

    /**
     * 购物车删除
     * */
    suspend fun cartDelete1(cartListIds: String): CartActionResponse =
        RxHttp.postForm("/api/cart/delete")
            .add("cart_list", cartListIds)
            .await()

    /**
     * 购物车结算
     * */
    suspend fun cartSettlement(
        cartIds: Array<Int>
    ): CartSettlementResponse = RxHttp.postJson("/api/cart/settlement")
        .add("cart_list", cartIds)
        .add("shop_id", "${JCApplication.userShops?.id?:"55"}")
        .await()

    /**
     * 立即购买
     * */
    suspend fun cartBuyNow(
        goodsId: String, goodsNum: String
    ): HttpResponseData = RxHttp.postForm("/api/cart/buyNow")
        .add("goods_id", goodsId)
        .add("goods_num", goodsNum)
        .add("shop_id", "${JCApplication.userShops?.id?:"55"}")
        .await()
}